using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Helpers;
	using System.Web.Security;
    using TemplateWeb.DTO;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectSanPham()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_SANPHAM
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectSanPhamByIDDanhMucCon(int iddanhmuccon)
		{
			var data = new DataTransferViewModel
			{
				IDDanhMucCon = iddanhmuccon,
				EnumViewModelType = EnumViewModel.ADMIN_SANPHAM,
			};
			return View("SelectSanPham", CreateViewModel(data));
		}

		public ActionResult SelectSanPhamByIDDanhMucConPaging(int iddanhmuccon)
		{
			var data = new DataTransferViewModel
			{
				IDDanhMucCon = iddanhmuccon,
				EnumViewModelType = EnumViewModel.ADMIN_SANPHAM,
			};
			return PartialView("Templates/TH_ListSanPham", CreateViewModel(data));
		}

		public ActionResult SelectSanPhamByIDToChucCaNhan(int idtochuccanhan)
		{
			var data = new DataTransferViewModel
			{
				IDToChucCaNhan = idtochuccanhan,
				EnumViewModelType = EnumViewModel.ADMIN_SANPHAM,
			};
			return View("SelectSanPham", CreateViewModel(data));
		}

		public ActionResult SelectSanPhamByIDToChucCaNhanPaging(int idtochuccanhan)
		{
			var data = new DataTransferViewModel
			{
				IDToChucCaNhan = idtochuccanhan,
				EnumViewModelType = EnumViewModel.ADMIN_SANPHAM,
			};
			return PartialView("Templates/TH_ListSanPham", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteSanPham(int id)
		{
			var deleteItem = _repSanPham.SelectByID(id);
			var fileName = deleteItem.HinhAnh;
			var iddanhmuccon = deleteItem.IDDanhMucCon;
			var idtochuccanhan = deleteItem.IDToChucCaNhan;
			var result = _repSanPham.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repSanPham.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfSanPham(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SANPHAM,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateSanPham(FormCollection forms)
		{
			string id = forms.GetValues("SanPham_ID")[0];
			string iddanhmuccon = forms.GetValues("SanPham_IDDanhMucCon")[0];
			string idtochuccanhan = forms.GetValues("SanPham_IDToChucCaNhan")[0];
			string tensanpham = forms.GetValues("SanPham_TenSanPham")[0];
			string xuatxu = forms.GetValues("SanPham_XuatXu")[0];
			string mota = forms.GetValues("SanPham_MoTa")[0];
			string soluongton = forms.GetValues("SanPham_SoLuongTon")[0];
			var file = Request.Files["SanPham_HinhAnh"];
			string refName = "";
			var editItem = _repSanPham.SelectByID(int.Parse(id));
			editItem.IDDanhMucCon = int.Parse(iddanhmuccon.Replace(".", "").Replace(",", ""));
			editItem.IDToChucCaNhan = int.Parse(idtochuccanhan.Replace(".", "").Replace(",", ""));
			editItem.TenSanPham = tensanpham;
			editItem.XuatXu = xuatxu;
			editItem.MoTa = mota;
			editItem.SoLuongTon = int.Parse(soluongton.Replace(".", "").Replace(",", ""));
			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", ref refName);
				editItem.HinhAnh = refName;
			}
			result = _repSanPham.Update(editItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SANPHAM,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfSanPham", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertSanPham(FormCollection forms)
		{
			string iddanhmuccon = forms.GetValues("SanPham_IDDanhMucCon")[0];
			string idtochuccanhan = forms.GetValues("SanPham_IDToChucCaNhan")[0];
			string tensanpham = forms.GetValues("SanPham_TenSanPham")[0];
			string xuatxu = forms.GetValues("SanPham_XuatXu")[0];
			string mota = forms.GetValues("SanPham_MoTa")[0];
			string soluongton = forms.GetValues("SanPham_SoLuongTon")[0];
			var file = Request.Files["SanPham_HinhAnh"];
			string refName = "";

			var newItem = new SanPhamDTO
			{
				IDDanhMucCon = int.Parse(iddanhmuccon),
				IDToChucCaNhan = int.Parse(idtochuccanhan),
				TenSanPham = tensanpham,
				XuatXu = xuatxu,
				MoTa = mota,
				SoLuongTon = int.Parse(soluongton),
				HinhAnh = "noimages.jpg"
			};

			var result = _repSanPham.Insert(newItem);

			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", ref refName);
					if (result)
					{
						var addedItem = _repSanPham.SelectByID(newItem.ID);
						addedItem.HinhAnh = refName;
						_repSanPham.Update(addedItem);
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDDanhMucCon = int.Parse(iddanhmuccon),
				IDToChucCaNhan = int.Parse(idtochuccanhan),
				EnumViewModelType = EnumViewModel.ADMIN_SANPHAM,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectSanPham", CreateViewModel(data));
		}


	}
}